package com.ss.android.article.base.feature.update.b;

import android.content.Context;
import android.os.Message;

import com.bytedance.article.common.c.TTUtils;
import com.bytedance.common.utility.Logger;
import com.bytedance.common.utility.NetworkType;
import com.bytedance.common.utility.collection.WeakHandler;
import com.bytedance.common.utility.collection.WeakList;
import com.ss.android.account.b.OnAccountRefreshListener;
import com.ss.android.account.Spipe;
import com.ss.android.article.base.app.AppData;
import com.ss.android.article.base.feature.update.a.e;
import com.ss.android.article.base.feature.update.a.f;
import com.ss.android.b.a.AdsAppItem;
import com.ss.android.common.AbsApiThread;
import com.ss.android.common.util.NetworkUtils;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;

import inline.PackageUtils;

public abstract class a<T extends com.ss.android.article.base.feature.update.a.a> implements WeakHandler.Callback, OnAccountRefreshListener {
    public interface a$a {
        void a(boolean arg1, int arg2, int arg3, AdsAppItem arg4);

        void a();

        void b();
    }

    final class b extends AbsApiThread {
        private final a b;
        private c a;

        public b(a arg2, c arg3) {
            super("UpdateListManager-Thread");
            this.b = arg2;
            this.a = arg3;
        }

        public final void run() {
            boolean v1_1;
            boolean v0 = false;
            a v2 = this.b;
            c v3 = this.a;
            try {
                v0 = v2.b(v3);
                v1_1 = v0;
            } catch (Throwable v1) {
                v3.i = TTUtils.getErrorCode(v2.e, v1);
                v1_1 = v0;
            }

            int v0_1 = 11;
            if (v1_1) {
                v0_1 = 10;
            }

            Message v0_2 = v2.p.obtainMessage(v0_1);
            v0_2.obj = v3;
            v2.p.sendMessage(v0_2);
        }
    }

    public final class c<T extends com.ss.android.article.base.feature.update.a.a> {
        public final int a;
        public final boolean b;
        public final long c;
        public final long d;
        public List<Long> e;
        public List<e<T>> f;
        public long g;
        public boolean h;
        public int i;
        public AdsAppItem j;
        public long k;
        public long l;
        public int m;
        public int n;

        public c(int arg2, boolean arg3, long arg4, long arg6, int arg8) {
            this.i = 18;
            this.a = arg2;
            this.b = arg3;
            this.c = arg4;
            this.d = arg6;
            this.n = arg8;
        }
    }

    private long a;
    private int b;
    public final Spipe c;
    public long d;
    public Context e;
    public final int f;
    public boolean g;
    public boolean h;
    public boolean i;
    public boolean j;
    public long k;
    public long l;
    public volatile boolean m;
    public final List<e<T>> n;
    public boolean o;
    WeakHandler p;
    public final List<e<T>> q;
    public WeakList r;
    private HashSet s;

    public a(Context arg7, int arg8) {

        this.h = false;
        this.k = 0;
        this.l = 0;
        this.a = -1;
        this.m = false;
        this.s = new HashSet();
        this.n = new ArrayList();
        this.o = false;
        this.p = new WeakHandler(((WeakHandler.Callback) this));
        this.q = new ArrayList();
        this.e = arg7.getApplicationContext();
        this.f = arg8;
        this.r = new WeakList();
        AppData.getInstance();
        this.c = Spipe.inst();
        this.c.addOnAccountRefreshListener(((OnAccountRefreshListener) this));
        this.d = 0;
        if (this.c.isLogin) {
            this.d = this.c.userId;
        }

        this.b();
    }

    public final void a(boolean arg10, int arg11) {
        if (!this.g) {
            this.i = arg10;
            this.g = true;
            ++this.b;
            long v4 = this.i ? this.k : this.l;
            new b(this, new c(this.b, this.i, v4, this.d, arg11)).start();
            Iterator v1 = this.r.iterator();
            while (v1.hasNext()) {
                Object v0 = v1.next();
                if (v0 == null) {
                    continue;
                }

                ((a$a) v0).b();
            }
        }
    }

    private static void a(boolean arg0) {
        if (arg0) {
            System.currentTimeMillis();
        }
    }

    public abstract com.ss.android.article.base.feature.update.a.a a(JSONObject arg1);

    public abstract e a(com.ss.android.article.base.feature.update.a.a arg1);

    public abstract String a(c arg1);

    public abstract void a(long arg1, long arg2, List arg3);

    public void a(List arg1) {
    }

    public final void a(boolean arg3, int arg4, int arg5, AdsAppItem arg6) {
        if (!this.r.weakHashMap.isEmpty()) {
            Iterator v1 = this.r.iterator();
            while (v1.hasNext()) {
                Object v0 = v1.next();
                if (v0 == null) {
                    continue;
                }

                ((a$a) v0).a(arg3, arg4, arg5, arg6);
            }
        }
    }

    public boolean a() {
        return false;
    }

    public abstract boolean a(int arg1);

    public abstract void b();

    final boolean b(c arg19) throws Throwable {
        long v16;
        if (NetworkUtils.getNetworkType(this.e) == NetworkType.NONE) {
            arg19.i = 12;
            boolean v2 = false;
            return v2;
        }

        if (!this.a(this.f)) {
            arg19.i = 18;
            return false;
        }

        String v2_1 = this.a(arg19);
        if (android.support.design.a.isTextEmpty(v2_1)) {
            return false;
        }

        JSONObject v3 = new JSONObject(v2_1);
        String v4 = v3.getString("message");
        if (!"success".equals(v4)) {
            if (("error".equals(v4)) && ("session_expired".equals(v3.getJSONObject("data").optString("name")))) {
                arg19.i = 105;
                return false;
            }

            Logger.w("UserListManager", "get update list failed: " + v2_1);
            return false;
        }

        ArrayList v8 = new ArrayList();
        JSONObject v9 = v3.getJSONObject("data");
        if (v9 == null) {
            return false;
        }

        JSONArray v7 = v9 != null ? v9.optJSONArray("data") : null;
        long v2_2 = 0;
        long v4_1 = 0;
        if (v7 != null && v7.length() > 0) {
            int v10 = v7.length();
            int v6 = 0;
            while (v6 < v10) {
                com.ss.android.article.base.feature.update.a.a v11 = this.a(v7.getJSONObject(v6));
                if (v11 != null) {
                    v11.r = System.currentTimeMillis();
                    e v11_1 = this.a(v11);
                    ((List) v8).add(v11_1);
                    if (v11_1.c > v2_2) {
                        v2_2 = v11_1.c;
                    }

                    if (v11_1.c <= 0) {
                        v16 = v4_1;
                        v4_1 = v2_2;
                        v2_2 = v16;
                    } else if (v4_1 > 0 && v11_1.c >= v4_1) {
                        v16 = v4_1;
                        v4_1 = v2_2;
                        v2_2 = v16;
                    } else {
                        v4_1 = v2_2;
                        v2_2 = v11_1.c;
                    }
                } else {
                    v16 = v4_1;
                    v4_1 = v2_2;
                    v2_2 = v16;
                }

                ++v6;
                v16 = v2_2;
                v2_2 = v4_1;
                v4_1 = v16;
            }
        }

        arg19.f = ((List) v8);
        arg19.h = AbsApiThread.getHasMore(v9, true);
        arg19.m = v9.optInt("login_status", -1);
        arg19.k = v2_2;
        arg19.l = v4_1;
        v2_2 = v9.optLong("min_cursor");
        v4_1 = v9.optLong("max_cursor");
        if (arg19.k < v2_2) {
            arg19.k = v2_2;
        }

        if ((arg19.l == 0 || arg19.l > v4_1) && v4_1 > 0) {
            arg19.l = v4_1;
        }

        JSONArray v2_3 = v9 != null ? v9.optJSONArray("change_list") : null;
        long[] v3_1 = android.support.design.a.c(v2_3);
        if (v3_1 != null && v3_1.length > 0) {
            arg19.e = new ArrayList();
            int v4_2 = v3_1.length;
            int v2_4;
            for (v2_4 = 0; v2_4 < v4_2; ++v2_4) {
                long v6_1 = v3_1[v2_4];
                if (v6_1 > 0) {
                    arg19.e.add(Long.valueOf(v6_1));
                }
            }
        }

        arg19.g = System.currentTimeMillis();
        if (arg19.b) {
            try {
                JSONObject v2_6 = v9.optJSONObject("tips");
                if (v2_6 == null) {
                    return true;
                }

                arg19.j = AdsAppItem.fromJO(v2_6);
            } catch (Exception v2_5) {
            }
        }

        return true;
    }

    public final void d() {
        if (!this.g && !this.h && (this.q.isEmpty()) && (this.c.isLogin) && this.a <= 0 && (NetworkUtils.isNetworkAvailable(this.e))) {
            this.a(true, 2);
        }
    }

    public void handleMsg(Message message) {
        boolean v1_1;
        int v2;
        AdsAppItem v9 = null;
        int v6 = -1;
        long v10 = 0;
        boolean v7 = true;
        int v1;
        switch (message.what) {
            case 10: {
                v1 = 1;
                break;
            }
            case 11: {
                v1 = 0;
                break;
            }
            default:
                return;
        }
        if ((message.obj instanceof com.ss.android.article.base.feature.update.b.a.c)) {
            Object v0 = ((c) message.obj);
            if (v0 == null) {
                return;
            }

            if (((c) v0).a != this.b) {
                return;
            }

            if (((c) v0).m < 0) {
                v2 = this.c.isLogin ? 1 : 0;
                ((c) v0).m = v2;
            }

            this.g = false;
            if (v1 == 0) {
                int v0_1 = ((c) v0).i;
                if (v0_1 <= 0) {
                    v0_1 = 18;
                }

                this.a(false, v0_1, v6, v9);
                return;
            }

            this.o = true;
            long v2_1 = ((c) v0).d;
            if (!this.q.isEmpty() && this.d != v2_1) {
                this.q.clear();
                this.k = v10;
                this.l = v10;
                if (this.r.weakHashMap.isEmpty()) {
                } else if (NetworkUtils.isNetworkAvailable(this.e)) {
                    this.a(true, ((c) v0).n);
                } else {
                    this.a(true, 12, v6, v9);
                }

                return;
            }

            List v2_2 = ((c) v0).f;
            if (v2_2 != null && v2_2.size() > 0) {
                this.a(v2_2);
            }

            if (this.i) {
                if (((c) v0).c <= v10) {
                    this.q.clear();
                    v1_1 = true;
                } else {
                    v1_1 = false;
                }

                List v3 = PackageUtils.a(this.q, v2_2);
                v2 = v3 == null || (v3.isEmpty()) ? 1 : 0;
                if (v2 != 0) {
                    if (this.k < ((c) v0).k) {
                        this.k = ((c) v0).k;
                    }

                    if (this.q.isEmpty()) {
                        this.j = false;
                        v7 = v1_1;
                        v1 = 0;
                    } else {
                        v7 = v1_1;
                        v1 = 0;
                    }
                } else {

                    int v9_1 = v3.size();
                    if (this.k < ((c) v0).k) {
                        this.k = ((c) v0).k;
                    }

                    if (((c) v0).h) {
                        this.j = true;
                    } else {
                        v3.addAll(this.q);
                    }

                    if ((((c) v0).h) || (this.q.isEmpty())) {
                        this.l = ((c) v0).l;
                    }

                    this.q.clear();
                    this.q.addAll(((Collection) v3));
                    this.s.clear();
                    ArrayList v6_1 = new ArrayList();
                    try {
                        boolean v3_1 = this.a();
                        Iterator v4 = this.q.iterator();
                        v2 = 0;
                        do {
                            if (!v4.hasNext()) {
                                break;
                            }

                            Object v1_3 = v4.next();
                            ++v2;
                            if (((e) v1_3).b != 1) {
                                continue;
                            }
                            this.s.add(Long.valueOf(((e) v1_3).a.i));
                            f v5 = new f();
                            JSONObject v10_1 = ((e) v1_3).a.a();
                            if (v3_1) {
                                v10_1.put("cursor", ((e) v1_3).c);
                                v10_1.put("create_time", ((e) v1_3).a.s);
                            }
                            v5.a = v10_1.toString();
                            ((List) v6_1).add(v5);
                        } while (((List) v6_1).size() < 30);

                        v1 = v2;
                        this.n.clear();
                        if (v1 > this.q.size()) {
                            v1 = this.q.size();
                        }

                        if (v1 > 0) {
                            this.n.addAll(this.q.subList(0, v1));
                        }

                        this.a(this.d, this.a, ((List) v6_1));
                        v1 = v9_1;
                    } catch (Exception v1_2) {
                        Logger.w("BaseUpdateListManager", "trySaveTopN exception: " + v1_2);
                        v1 = v9_1;
                    }
                }
                this.a = ((c) v0).g;
                this.a(v7, 0, v1, ((c) v0).j);
                a(false);
                return;
            }

            if (((c) v0).l > v10 && (this.l <= v10 || this.l > ((c) v0).l)) {
                this.l = ((c) v0).l;
            }

            this.j = ((c) v0).h;
            List v1_4 = PackageUtils.a(this.q, v2_2);
            if (v1_4 == null || (v1_4.isEmpty())) {
                v1_1 = false;
            } else {
                this.q.addAll(((Collection) v1_4));
                v1_1 = true;
            }

            if (!this.j || (v1_1)) {
                v7 = false;
            }

            a(v7);
            this.a(v1_1, 0, v6, ((c) v0).j);
        }
    }

    public void onAccountRefresh(boolean arg9, int arg10) {
        long v4 = 0;
        long v0 = this.d;
        this.d = v4;
        if (this.c.isLogin) {
            this.d = this.c.userId;
        }

        if (v0 != this.d) {
            if (this.g) {
                this.g = false;
                ++this.b;
            }

            this.q.clear();
            this.a = v4;
            this.k = v4;
            this.l = v4;
            this.j = true;
            this.a(true, 0, -1, null);
        }

        if (!this.c.isLogin) {
            this.j = false;
        }

        Iterator v1 = this.r.iterator();
        while (v1.hasNext()) {
            Object v0_1 = v1.next();
            if (v0_1 == null) {
                continue;
            }

            ((a$a) v0_1).a();
        }
    }
}

